package admin

import (
	"SSO/controllers"
	"SSO/models"
	"fmt"
	"github.com/gin-gonic/gin"
)

type User struct {
	controllers.Controller
}

func (this User) UpdatePassword(c *gin.Context)  {
	nowUsername := this.SessionGet("username", c).(string)
	b, err := models.Admins{}.IsAdmin(nowUsername)
	if err != nil {
		this.ReturnJson(-1, "验证权限失败", nil, c)
		return
	}

	if !b {
		this.ReturnJson(-1, "无权操作", nil, c)
		return
	}
	username := this.GetDefaultStr("username", "", c)
	password := this.GetDefaultStr("password", "", c)


	if err := (models.Users{}.CheckUsername(username)); err != nil {
		this.ReturnJson(-1, err.Error(), nil, c)
		return
	}

	if err := (models.Users{}.CheckPassword(password));err != nil {
		this.ReturnJson(-1, err.Error(), nil, c)
		return
	}
	err = models.Users{}.UpdatePassword(username, password)
	if err != nil {
		msg := fmt.Sprintf("修改密码失败 err[%s]", err.Error())
		this.ReturnJson(-1, msg, nil, c)
		return
	}
	this.ReturnJson(0, "success", nil, c)
}
